package com.nulstudio.sppm.Controller;

import com.alibaba.fastjson2.JSONObject;
import com.nulstudio.sppm.Annotation.NormalJwt;
import com.nulstudio.sppm.Dao.CommodityMapper;
import com.nulstudio.sppm.Dao.InventoryEntryMapper;
import com.nulstudio.sppm.Entities.Commodity;
import com.nulstudio.sppm.Entities.InventoryEntry;
import com.nulstudio.sppm.Entities.InventoryRequest;
import com.nulstudio.sppm.Entities.invoicePost;
import com.nulstudio.sppm.Service.CommodityService;
import com.nulstudio.sppm.Service.InventoryEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RestController
@RequestMapping("/service/inventory")
public class InventoryEntryController {
    @Autowired
    private InventoryEntryService warehouseService;
    @Autowired
    private CommodityMapper commodityMapper;
    @NormalJwt
    @PostMapping(value = "/fetchAll",produces = "application/json;charset=UTF-8")
    public String fetchAll(@RequestBody Map<String,String> map) {
        JSONObject json = new JSONObject();
        final String warehouseId = map.get("warehouseId");

        List<InventoryEntry> supplierList = warehouseService.getAllWarehouses(Integer.parseInt(warehouseId));
        json.put("code",100);
        json.put("message", "");
        json.put("version",1);
        json.put("result", supplierList);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value ="/createEntry",produces = "application/json;charset=UTF-8")
    public String createEntry(@RequestBody Map<String,String> map) {
        JSONObject json = new JSONObject();
        String[] properties = new String[]{"id","quantity","supplierId"};
        for (String property : properties) {
            if (!map.containsKey(property)) {
                json.put("code", 400);
                json.put("message", property + " missed");
                json.put("version", 1);
                json.put("result", "");
                return json.toJSONString();
            }
        }
        Commodity commodity = commodityMapper.getCommodityById(Integer.parseInt(map.get(properties[0])));
        if(commodity != null) {
            InventoryEntry warehouse = new InventoryEntry();
            warehouse.setId(Integer.parseInt(map.get(properties[0])));
            warehouse.setQuantity(Integer.parseInt(map.get(properties[1])));
            warehouse.setSupplierName(map.get(properties[2]));
            warehouse.setName(commodity.getName());
            warehouse.setCostPrice(commodity.getCostPrice());
            warehouse.setRetailPrice(commodity.getRetailPrice());
            warehouse.setWholesalePrice(commodity.getWholesalePrice());


            final int supplierId = warehouseService.addWarehouse(warehouse);
            warehouse.setId(supplierId);
            json.put("code", 0);
            json.put("message", "");
            json.put("version", 1);
            json.put("result", warehouse);
            return json.toJSONString();
        }
        json.put("code", 1);
        json.put("message", "nonexisted commodity");
        json.put("version", 1);
        json.put("result", "");
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value ="/modifyEntry",produces = "application/json;charset=UTF-8")
    public String modifyEntry(@RequestBody Map<String,String> map) {
        JSONObject json = new JSONObject();
        InventoryEntry warehouse = new InventoryEntry();
        String[] properties = new String[]{"supplierName","nid","warehouseId"};
        if (map != null) {
            warehouse.setId(Integer.parseInt(map.get(properties[2])));
            warehouse.setQuantity(Integer.parseInt(map.get(properties[0])));
            Commodity commodity = commodityMapper.getCommodityById(Integer.parseInt(map.get(properties[2])));
            if(commodity != null) {
                warehouse.setName(commodity.getName());
                warehouse.setSupplierName(map.get(properties[1]));
                warehouse.setCostPrice(commodity.getCostPrice());
                final int nid = Integer.parseInt(map.get(properties[2]));
                warehouseService.modifyWarehouse(warehouse, nid);
                json.put("code", 0);
                json.put("message", "");
                json.put("version", 1);
                json.put("result", "");
                return json.toJSONString();
            }
            json.put("code", 1);
            json.put("message", "commodity missed");
            json.put("version", 1);
            json.put("result", "");
            return json.toJSONString();
        }
        json.put("code", 401);
        json.put("message", "no modify request received");
        json.put("version", 1);
        json.put("result","");
        return json.toJSONString();
    }
    @NormalJwt
    @PostMapping(value ="/deleteEntry",produces = "application/json;charset=UTF-8")
    public String deleteEntry(@RequestBody Map<String, Object> map){
        JSONObject json = new JSONObject();
        final String id = (String)map.get("id");
        warehouseService.deleteWarehouse(Integer.parseInt(id));
        json.put("code", 0);
        json.put("message", "");
        json.put("result", "");
        json.put("version", 1);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value ="/transferEntry",produces = "application/json;charset=UTF-8")
    public String transferEntry(@RequestBody Map<String, Object> map){
        JSONObject json = new JSONObject();
        final int targetId = (int)map.get("targetId");
        List<LinkedHashMap> list = (List<LinkedHashMap>)map.get("transferEntries");
        for(LinkedHashMap req : list){
            warehouseService.transferEntry(targetId,(int)req.get("warehouseId"),
                    (int)req.get("commodityId"),(int)req.get("quantity"));
        }
        json.put("code", 0);
        json.put("message", "");
        json.put("result", "");
        json.put("version", 1);
        return json.toJSONString();
    }

}
